package com.xingxue.util;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public abstract class Page implements Map<String, Object> {

	private Map<String, Object> map = new HashMap<>();

	public int size() {
		return map.size();
	}

	public boolean isEmpty() {
		return map.isEmpty();
	}

	public boolean containsKey(Object key) {
		return map.containsKey(key);
	}

	public boolean containsValue(Object value) {
		return map.containsValue(value);
	}

	public Object get(Object key) {
		return map.get(key);
	}

	public Object put(String key, Object value) {
		return map.put(key, value);
	}

	public Object remove(Object key) {
		return map.remove(key);
	}

	public void putAll(Map<? extends String, ? extends Object> m) {
		map.putAll(m);
	}

	public void clear() {
		map.clear();
	}

	public Set<String> keySet() {
		return map.keySet();
	}

	public Collection<Object> values() {
		return map.values();
	}

	public Set<java.util.Map.Entry<String, Object>> entrySet() {
		return map.entrySet();
	}

	public boolean equals(Object o) {
		return map.equals(o);
	}

	public int hashCode() {
		return map.hashCode();
	}

	private int page;
	private int pageSize;

	/**
	 * 构造方法
	 * 
	 * @param page
	 *            页号
	 * @param rows
	 *            每页记录数
	 */
	public Page(int page, int rows) {
		super();
		this.page = page;
		this.pageSize = rows;
		this.put("page", page);
		this.put("pageSize", rows);
		setBounds(this.page, this.pageSize);
	}

	protected abstract void setBounds(int page, int rows);

	/**
	 * 添加条件
	 * 
	 * @param key
	 *            名称
	 * @param value
	 *            值
	 * @return
	 */
	public Page addCondition(String key, Object value) {
		this.put(key, value);
		return this;
	}

	private int total;
	private int totalPages;
	private List<?> rows;

	/**
	 * 获取本页记录集合
	 * 
	 * @return
	 */
	public List<?> getRows() {
		return rows;
	}

	/**
	 * 获取当前页号
	 * 
	 * @return
	 */
	public int getPage() {
		return page;
	}

	/**
	 * 获取每页记录数
	 * 
	 * @return
	 */
	public int getPageSize() {
		return pageSize;
	}

	/**
	 * 获取总记录数
	 * 
	 * @return
	 */
	public int getTotal() {
		return total;
	}

	/**
	 * 设置本页记录集合和总记录数
	 * 
	 * @param rows
	 * @return
	 */
	public Page setRowsAndTotal(List<?> rows, int total) {
		this.rows = rows;
		this.total = total;
		this.put("rows", rows);
		this.put("total", total);
		this.totalPages = total % pageSize == 0 ? total / pageSize : total / pageSize + 1;
		this.put("totalPages", totalPages);
		calulateBeginEnd();
		return this;
	}

	/**
	 * 获取总页数
	 * 
	 * @return
	 */
	public int getTotalPages() {
		return totalPages;
	}

	private int pageBegin;
	private int pageEnd;

	/**
	 * 获取起始页码数
	 * 
	 * @return
	 */
	public int getPageBegin() {
		return pageBegin;
	}

	/**
	 * 获取结束页码数
	 * 
	 * @return
	 */
	public int getPageEnd() {
		return pageEnd;
	}

	private int pageMax = 10; // 显示的最多页码数
	private int pageMaxLeft = 5; // 当前页左侧的最多页码数

	private void calulateBeginEnd() {
		int right = pageMax - pageMaxLeft - 1;
		if (totalPages <= pageMax) {
			pageBegin = 1;
			pageEnd = totalPages;
		} else {
			if (page <= pageMaxLeft + 1) {
				pageBegin = 1;
				pageEnd = pageMax;
			} else {
				if (totalPages - page <= right) {
					pageBegin = totalPages - (pageMax - 1);
					pageEnd = totalPages;
				} else {
					pageBegin = page - pageMaxLeft;
					pageEnd = page + right;
				}
			}
		}
		this.put("pageBegin", pageBegin);
		this.put("pageEnd", pageEnd);
	}

}
